var actionsinfoManager;
var actionsinfoItemPageOpen;
var actionsinfoItem;
var loadDataInitParent = {id:0,type:0};
function changeInitParent(idc,namec,typec){
	loadDataInitParent = {id:idc,name:namec,type:typec};
}
function initActionsInfoManager(){
	var colimnsArray = [];
		colimnsArray.push({
			display : '编号',
			name : 'id',
			align : 'left',
			showTitle : true,
			minWidth : 50,
			align : "center",
			editor: { type: 'text'},
			isSort : false
		});
		colimnsArray.push({
			display : '名称',
			name : 'name',
			align : 'left',
			showTitle : true,
			minWidth : 50,
			align : "center",
			editor: { type: 'text'},
			isSort : false
		});
		colimnsArray.push({
			display : '类型',
			name : 'type',
			align : 'left',
			showTitle : true,
			minWidth : 50,
			align : "center",
			editor: { type: 'select', data: actionTypesRenderData, valueColumnName: 'value' },
			render:function(item){ return defaultRenderFun(item.type,actionTypesRenderData);}
		});
		colimnsArray.push({
			display : '路径',
			name : 'path',
			align : 'left',
			showTitle : true,
			minWidth : 50,
			align : "center",
			editor: { type: 'text'},
			isSort : false
		});
		colimnsArray.push({
			display : '上级',
			name : 'parent.name',
			align : 'left',
			minWidth : 50,
			align : "center",
			render:function(item){
				if(item.parent){
					return "<a href='#' onClick='actionsinfoManager.loadServerData({id:"+item.parent.id+"});changeInitParent("+item.parent.id+",\""+item.parent.name+"\","+item.parent.type+");'>"+item.parent.name+"</a>";
				}else{
					return "<a href='#' onClick='actionsinfoManager.loadServerData({pid:0});changeInitParent(0,\"\",0);'>根菜单</a>";
				}
			}
		});
		
		colimnsArray.push({
			display : '操作',
			align : 'left',
			showTitle : false,
			minWidth : 200,
			align : "center",
			render : function(rowdata, rowindex) {
				if(rowdata.type!=3){
					return " <a  href='#' onClick='openActionsInfoItemPage(\"create\",\"新增功能菜单\",{pid:"+rowdata.id+",type:"+(rowdata.type+1)+"});'>增加子项</a>" +
							"  |  <a  href='#' onClick='actionsinfoManager.loadServerData({pid:"+rowdata.id+"});changeInitParent("+rowdata.id+",\""+rowdata.name+"\","+rowdata.type+");'>查看子项</a>" +
							"  |  <a  href='#' onClick='removeActionsInfo("+rowdata.id+");'>删除</a>";
				}else{
					return " <a  href='#' onClick='removeActionsInfo("+rowdata.id+");'>删除</a>";
				}
			}
		});
	
	var menu = $.ligerMenu({ width: 120, items:
		[
			{ text: '增加', click: function(){openActionsInfoItemPage('create',"新增菜单",{type:1,pid:0})}, icon: 'add' },
			{ line: true },
			{ text: '修改', click: function(){openActionsInfoItemPage('edit',"编辑")} },
			{ line: true },
			{ text: '查看', click: function(){openActionsInfoItemPage('show',"查看")} }
		]
	}); 
	
	window['g'] = jQuery("#maingrid").ligerGrid({
		height : '100%',
		url:'/admin/security/actionsInfo/ajaxList.do',
		columns : colimnsArray,
		pageSize : 10,
		rownumbers : true,
		toolbar : {
			items : [ {
				text : '保存',
				click : saveUpdateActionsInfoManager,
				icon : 'modify'
			}, {
				line : true
			}, {
				text : '增加',
				click : function(){openActionsInfoItemPage('create',"新增菜单",{type:1,pid:0})},
				icon : 'add'
			}, {
				line : true
			}]
		},
		onContextmenu : function (parm,e)
        {
			actionsinfoItem = parm.data;
	        menu.show({ top: e.pageY, left: e.pageX });
	        return false;
        },
        enabledEdit: false, 
        isScroll: false, 
        delayLoad:true,
        rownumbers:true
	});
	window['g'].loadServerData({pagesize:10,pid:loadDataInitParent.id});
	return window['g'];
};

function saveUpdateActionsInfoManager(item){
	var data = actionsinfoManager.getUpdated();
	if(data.length == 0){
		$.ligerDialog.warn("您还未做任何修改哦!");
		return;
	}
	doSaveActionsInfos(data);
};
function removeActionsInfo(actionid){
	jQuery.ajax({
		url:'/admin/security/actionsInfo/ajaxDelete.do',
		data:{id:actionid},
		dataType:'text',
		type:'post',
		success:function(data){
			if(data == 'success'){
				$.ligerDialog.success('恭喜,已经删除成功!');
				tip = $.ligerDialog.tip({ title: '提示信息', content: '恭喜,已经删除了1条记录!' });
				actionsinfoManager.loadServerData({pagesize:10,pid:loadDataInitParent.id});
			}else{
				tip = $.ligerDialog.tip({ title: '提示信息', content: '网络异常,没有保存成功!' });
			}
		},
		error:function(){
			$.ligerDialog.error("网络发生异常,请稍后再试!");
		}
	});
}
function doSaveActionsInfos(newData,add){
	jQuery.ajax({
		url:'/admin/security/actionsInfo/saveOrUpdate.do',
		data:{json:JSON.stringify(newData)},
		dataType:'json',
		type:'post',
		success:function(data){
			if(data.sucessList.length > 0){
				$.ligerDialog.success('恭喜,已经保存成功!');
				tip = $.ligerDialog.tip({ title: '提示信息', content: '恭喜,已经保存'+data.sucessList.length+'条记录!' });
				actionsinfoManager.loadServerData({pagesize:10,pid:loadDataInitParent.id});
				actionsinfoItemPageOpen.hidden();
				jQuery("#openDiv").html('');
			}else{
				if(data.msg){
					tip = $.ligerDialog.tip({ title: '提示信息', content: data.msg });
				}else{
					tip = $.ligerDialog.tip({ title: '提示信息', content: '网络异常,没有保存成功!' });
				}
			}
		},
		error:function(){
			$.ligerDialog.error("网络发生异常,请稍后再试!");
		}
	});
};
/**
 * 
 * @param item
 * @param target
 */
function openActionsInfoItemPage(target,titleName,optionsJson){
	if(!optionsJson){
		optionsJson = {};
	}
	optionsJson.id = (actionsinfoItem?actionsinfoItem.id:0);
	jQuery.ajax({
		url:'/admin/security/actionsInfo/'+target+'.do',
		data:optionsJson,
		dataType:'text',
		type:'post',
		success:function(data){
			if(data){
				jQuery("#openDiv").html(data);
				actionsinfoItemPageOpen = $.ligerDialog.open({ target: $("#openDiv"),isResize:true,title:titleName,width:450 });
			}
		},
		error:function(){
			$.ligerDialog.error("网络发生异常,请稍后再试!");
		}
	});
};

jQuery(function() {
	actionsinfoManager = initActionsInfoManager();
    jQuery("#pageloading").hide();
});